package com.fly.doc.generator.process;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.jdbc.core.JdbcTemplate;

import com.zaxxer.hikari.HikariDataSource;

import cn.wuwenyao.db.doc.generator.config.ApplicationConfig;
import cn.wuwenyao.db.doc.generator.config.GeneratorConfig;
import cn.wuwenyao.db.doc.generator.dao.DbInfoDao;
import cn.wuwenyao.db.doc.generator.dao.impl.dbinfo.mysql.MysqlDbInfoDao;
import cn.wuwenyao.db.doc.generator.enums.DbType;
import cn.wuwenyao.db.doc.generator.enums.TargetFileType;
import cn.wuwenyao.db.doc.generator.service.GeneratorService;
import cn.wuwenyao.db.doc.generator.service.impl.ExcelGeneratorServiceImpl;

/**
 * 
 * 刷新数据库表进度条线程
 * 
 * @author 00fly
 * @version [版本号, 2017年5月3日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class GenerateDocProgress implements IRunnableWithProgress
{
    
    private String url;
    
    private String userName;
    
    private String passWord;
    
    private String driver;
    
    private String docSaveDir;
    
    public GenerateDocProgress(String url, String userName, String passWord, String driver, String docSaveDir)
    {
        super();
        this.url = url;
        this.userName = userName;
        this.passWord = passWord;
        this.driver = driver;
        this.docSaveDir = docSaveDir;
    }
    
    @Override
    public void run(IProgressMonitor monitor)
        throws InvocationTargetException, InterruptedException
    {
        monitor.beginTask("刷新数据", IProgressMonitor.UNKNOWN);
        try
        {
            generateDoc(driver, url, userName, passWord, docSaveDir);
            monitor.subTask("生成数据库文档中......");
            monitor.done();
        }
        catch (Exception e)
        {
            throw new InvocationTargetException(e.getCause(), "生成数据库文档失败!");
        }
    }
    
    /**
     * generateDoc
     * 
     * @param driver
     * @param url
     * @param username
     * @param password
     * @param docSaveDir
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    private void generateDoc(String driver, String url, String username, String password, String docSaveDir)
        throws Exception
    {
        // 创建基础hikari数据源
        DataSourceBuilder<HikariDataSource> hikariDataSourceBuilder = DataSourceBuilder.create().type(HikariDataSource.class);
        HikariDataSource hikariDataSource = hikariDataSourceBuilder.driverClassName(driver).url(url).username(username).password(password).build();
        // 配置Hikari连接池
        hikariDataSource.setAutoCommit(true);// update自动提交设置
        hikariDataSource.setConnectionTestQuery("select 1");// 连接查询语句设置
        hikariDataSource.setConnectionTimeout(3000);// 连接超时时间设置
        hikariDataSource.setIdleTimeout(3000);// 连接空闲生命周期设置
        hikariDataSource.setIsolateInternalQueries(false);// 执行查询启动设置
        hikariDataSource.setMaximumPoolSize(3000);// 连接池允许的最大连接数量
        hikariDataSource.setMaxLifetime(1800000);// 检查空余连接优化连接池设置时间,单位毫秒
        hikariDataSource.setMinimumIdle(10);// 连接池保持最小空余连接数量
        hikariDataSource.setPoolName("hikariPool");// 连接池名称
        
        GeneratorConfig generator = new GeneratorConfig();
        generator.setDbtype(DbType.MYSQL);
        generator.setTargetFileType(TargetFileType.EXCEL);
        generator.setTargetFileDir(docSaveDir);
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setGenerator(generator);
        
        DbInfoDao dbInfoDao = new MysqlDbInfoDao();
        dbInfoDao.setJdbcTemplate(new JdbcTemplate(hikariDataSource));
        dbInfoDao.setApplicationConfig(applicationConfig);
        
        GeneratorService generatorService = new ExcelGeneratorServiceImpl();
        generatorService.setDbInfoDao(dbInfoDao);
        generatorService.setGeneratorConfig(generator);
        generatorService.generate();
    }
    
}